package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.OrderDetailService;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.service.UserService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@Api(tags = "数据统计相关接口")
@RequestMapping("/admin/report")
@RestController
@Slf4j
public class ReportController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 营业额统计
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return 营业额统计数据
     */
    @ApiOperation(value = "营业额统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "begin", value = "开始日期", required = true, paramType = "query", dataType = "localDate"),
            @ApiImplicitParam(name = "end", value = "结束日期", required = true, paramType = "query", dataType = "localDate")
    })
    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> getTurnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计，开始日期：{}，结束日期：{}", begin, end);
        TurnoverReportVO turnoverReportVO = orderService.getTurnoverStatistics(begin, end);
        return Result.success(turnoverReportVO);
    }

    /**
     * 用户统计
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return 用户统计数据
     */
    @ApiOperation(value = "用户统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "begin", value = "开始日期", required = true, paramType = "query", dataType = "localDate"),
            @ApiImplicitParam(name = "end", value = "结束日期", required = true, paramType = "query", dataType = "localDate")
    })
    @GetMapping("/userStatistics")
    public Result<UserReportVO> getUserStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        log.info("用户统计，开始日期：{}，结束日期：{}", begin, end);
        UserReportVO userReportVO = userService.getUserStatistics(begin, end);
        return Result.success(userReportVO);
    }

    /**
     * 订单统计
     *
     * @param begin 开始日期
     * @param end   结束日期
     * @return 订单统计数据
     */
    @ApiOperation(value = "订单统计")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "begin", value = "开始日期", required = true, paramType = "query", dataType = "localDate"),
            @ApiImplicitParam(name = "end", value = "结束日期", required = true, paramType = "query", dataType = "localDate")
    })
    @GetMapping("/ordersStatistics")
    public Result<OrderReportVO> getOrdersReport(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        log.info("订单统计，开始日期：{}，结束日期：{}", begin, end);
        OrderReportVO orderReportVO = orderService.getOrdersReport(begin, end);
        return Result.success(orderReportVO);
    }

    /**
     * 销量排名top10
     * @param begin 开始日期
     * @param end   结束日期
     * @return 销量排名top10数据
     */
    @ApiOperation(value = "销量排名top10")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "begin", value = "开始日期", required = true, paramType = "query", dataType = "localDate"),
            @ApiImplicitParam(name = "end", value = "结束日期", required = true, paramType = "query", dataType = "localDate")
    })
    @GetMapping("/top10")
    public Result<SalesTop10ReportVO> getSalesTop10(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
    ) {
        log.info("销量排名top10，开始日期：{}，结束日期：{}", begin, end);
        SalesTop10ReportVO salesTop10ReportVO = orderDetailService.getSalesTop10(begin, end);
        return Result.success(salesTop10ReportVO);
    }

    /**
     * 导出运营数据
     * @param response HttpServletResponse
     * @return excel文件
     */
    @ApiOperation(value = "导出运营数据")
    @GetMapping("/export")
    public Result<Object> exportBusinessData(HttpServletResponse response) {
        log.info("导出运营数据");
        reportService.exportBusinessData(response);
        return Result.success();
    }

}
